package com.canxin.controller;
import java.io.IOException;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import org.jeecgframework.core.common.controller.BaseController;
import org.jeecgframework.core.common.hibernate.qbc.CriteriaQuery;
import org.jeecgframework.core.common.model.json.AjaxJson;
import org.jeecgframework.core.common.model.json.DataGrid;
import org.jeecgframework.core.constant.Globals;
import org.jeecgframework.core.util.StringUtil;
import org.jeecgframework.tag.core.easyui.TagUtil;
import org.jeecgframework.web.system.pojo.base.TSDepart;
import org.jeecgframework.web.system.service.SystemService;
import org.jeecgframework.core.util.MyBeanUtils;

import com.alibaba.fastjson.JSONArray;
import com.canxin.entity.ExamEntity;
import com.canxin.entity.ScoreEntity;
import com.canxin.entity.StudentsEntity;
import com.canxin.service.ExamServiceI;
import com.canxin.service.ScoreServiceI;

/**   
 * @Title: Controller
 * @Description: 考试信息表
 * @author zhangdaihao
 * @date 2017-03-05 22:36:09
 * @version V1.0   
 *
 */
@Scope("prototype")
@Controller
@RequestMapping("/examController")
public class ExamController extends BaseController {
	/**
	 * Logger for this class
	 */
	private static final Logger logger = Logger.getLogger(ExamController.class);

	@Autowired
	private ExamServiceI examService;
	@Autowired
	private ScoreServiceI scoreService;
	
	@Autowired
	private SystemService systemService;
	private String message;
	
	public String getMessage() {
		return message;
	}

	public void setMessage(String message) {
		this.message = message;
	}

	/**
	 * 考试信息表列表 页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "exam")
	public ModelAndView exam(HttpServletRequest request) {
		return new ModelAndView("com/canxin/exam/examList");
	}

	/**
	 * easyui AJAX请求数据
	 * 
	 * @param request
	 * @param response
	 */

	@RequestMapping(params = "getExamList")
	@ResponseBody
	public AjaxJson getExamList(HttpServletRequest request, HttpServletResponse response) {
		List examList = examService.getAllExamsOrderByExamDate();
		AjaxJson result = new AjaxJson();
		result.setObj(examList);
		return result;
	}

	/**
	 * 删除考试信息表
	 * 
	 * @return
	 */
	@RequestMapping(params = "del")
	@ResponseBody
	public AjaxJson del(ExamEntity exam, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		exam = systemService.getEntity(ExamEntity.class, exam.getExamid());
		message = "考试信息表删除成功";
		examService.delete(exam);
		systemService.addLog(message, Globals.Log_Type_DEL, Globals.Log_Leavel_INFO);
		
		j.setMsg(message);
		return j;
	}


	/**
	 * 添加考试信息表
	 * 
	 * @return
	 */
	@RequestMapping(params = "save")
	@ResponseBody
	public AjaxJson save(ExamEntity exam, HttpServletRequest request) {
		AjaxJson j = new AjaxJson();
		if (StringUtil.isNotEmpty(exam.getExamid())) {
			message = "考试信息表更新成功";
			ExamEntity t = examService.get(ExamEntity.class, exam.getExamid());
			try {
				MyBeanUtils.copyBeanNotNull2Bean(exam, t);
				examService.saveOrUpdate(t);
				systemService.addLog(message, Globals.Log_Type_UPDATE, Globals.Log_Leavel_INFO);
			} catch (Exception e) {
				e.printStackTrace();
				message = "考试信息表更新失败";
			}
		} else {
			message = "考试信息表添加成功";
			examService.save(exam);
			systemService.addLog(message, Globals.Log_Type_INSERT, Globals.Log_Leavel_INFO);
		}
		j.setMsg(message);
		return j;
	}

	/**
	 * 考试信息表列表页面跳转
	 * 
	 * @return
	 */
	@RequestMapping(params = "addorupdate")
	public ModelAndView addorupdate(ExamEntity exam, HttpServletRequest req) {
		if (StringUtil.isNotEmpty(exam.getExamid())) {
			exam = examService.getEntity(ExamEntity.class, exam.getExamid());
			req.setAttribute("examPage", exam);
		}
		return new ModelAndView("com/canxin/exam/exam");
	}
}
